package com.mayblackcat.dy.service.impl;

import com.mayblackcat.dy.dao.LoginDao;
import com.mayblackcat.dy.dao.UserDao;
import com.mayblackcat.dy.dao.VideoDao;
import com.mayblackcat.dy.entity.User;
import com.mayblackcat.dy.entity.Video;
import com.mayblackcat.dy.exception.NotFoundException;
import com.mayblackcat.dy.service.UserService;
import com.mayblackcat.dy.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private VideoDao videoDao;

    @Override
    @Transactional
    public void updateUserAll(User user) {
        if (loginDao.getAvatarById(user.getId())!=null){
           userDao.updateUserAll(user);

        }else {
            throw new NotFoundException("该用户不存在");
        }

    }

    @Override
    @Transactional
    public void updateWatchVideo(User user) {
        userDao.updateWatchVideo(user);
    }

    //观看历史
    @Override
    public List<Video> getWatchVideoByIds(String ids) {
        List<Video> videoList = new ArrayList<>();
        if(!"".equals(ids) &&ids!=null){
            String[] split = ids.split(",");
            for (int i = 0; i < split.length; i++) {
               videoList.add( videoDao.getWatchVideoByIds(new Long(split[i])));
            }
        }

        return videoList;
    }

    @Override
    public User getUserById(long id) {
        return userDao.getUserById(id);
    }


}
